package com.etc;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.codingapi.txlcn.tc.annotation.LcnTransaction;

@Service
public class ServiceA {
    
    @Autowired
    private ValueDao valueDao; //本地db操作
    
    @Autowired
    private ServiceB serviceB;//远程B模块业务
    
    @LcnTransaction //分布式事务注解
    @Transactional //本地事务注解
    public String execute(String value) throws BusinessException {
        // step1. call remote service B
        String result = serviceB.rpc(value);  // (1)
        // step2. local store operate. DTX commit if save success, rollback if not.
        valueDao.save(value);  // (2)
//        valueDao.saveBackup(value);  // (3)
        return result + " > " + "ok-A";
    }
    
    @Autowired
    ServiceB2 serviceB2;
    
    @LcnTransaction //分布式事务注解
    @Transactional //本地事务注解
    public String execute1(String value) throws BusinessException {
        // step1. call remote service B
        String result = serviceB2.rpc(value);  // (1)
        // step2. local store operate. DTX commit if save success, rollback if not.
        valueDao.save(value);  // (2)
        if (1 == 1){
        	   throw new BusinessException("测试异常");
        }
     
//        valueDao.saveBackup(value);  // (3)
        return result + " > " + "ok-A";
    }
}